<template >
  <el-form :model="form" label-width="120px">
    <el-form-item label="图片地址" required>
      <el-input v-model="form.img" placeholder="请输入图片地址" />
    </el-form-item>
    <el-form-item label="图片提示">
      <el-input v-model="form.alt" placeholder="请输入图片提示" />
    </el-form-item>
    <el-form-item label="图片链接" required>
      <el-input v-model="form.link" placeholder="请输入图片链接" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">添加</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang='ts'>
import { reactive, ref } from "vue"; //解构
import { ElMessage } from "element-plus";
import service from "../../../utils/service"; //引入拦截器
import "element-plus/dist/index.css";
import { useRouter } from "vue-router";
const $router = useRouter();
const form = reactive({
  img: "",
  alt: "",
  link: "",
});

const onSubmit = () => {
  if (form.img) {
    service({
      url: "/banner/add",
      method: "post",
      data: form,
    }).then((res) => {
      if (res.data.code === "200") {
        ElMessage({
          message: "亲，恭喜您，添加成功!",
          type: "success",
        });
        $router.push("/home/list");
      }
    });
  } else {
    ElMessage({
      message: "亲,请填写图片地址哦",
      type: "error",
    });
  }
};
</script>

<style scoped>
.el-button {
  width: 200px;
  height: 40px;
  background-color: darkgray;
}
.el-input {
  width: 400px;
}
</style>